/* **************************************************************************/
/*COPYRIGHT CCWIN-IN ALL RIGHTS RESERVED. THIS IS AN UNPUBLISHED WORK.      */
/*V8:ConvertMode=Report                                                     */
/*V8:WebEnabled=Yes                                                         */
/*V8:RunMode=Character,Windows                                              */
/*EXTRACTING DATA FROM MFG/PRO                                              */
/****************************************************************************/
/* PROCEDURE NAME        : xxdeptapi.p                                      */
/* PROCEDURE TYPE        : Department CIM-LOAD API                          */
/* DESCRIPTION           : Department CIM-LOAD API                          */
/*                                                                          */     
/* INCLUDE FILES         :                                                  */
/* CALLED BY             :                                                  */
/* CALLED PROCEDURES     :                                                  */
/****************************************************************************/
/* CREATED BY            : Gaoyan                               26-OCT-24   */
/****************************************************************************/ 
/**   **/
/**   **/
/****************************************************************************/

{us/mf/mfdtitle.i}
{us/bbi/gplabel.i}
{us/wb/wbrp01.i}

{xxjson.i}
{xxcimload.i}

{xxapilog.i}


&SCOPED-DEFINE TimeStamp1 substring(string(year(today), "9999"),3) + string(month(today), "99") + string(day(today), "99")
&SCOPED-DEFINE TimeStamp2 substring(string(time, "HH:MM:SS"), 1, 2) + substring(string(time, "HH:MM:SS"), 4, 2) + substring(string(time, "HH:MM:SS"), 7, 2)

define input parameter vc_inputjson             as character format "x(60)" no-undo.
define input parameter vc_outputjson            as character format "x(60)" no-undo.
define input parameter vc_import                as logical  no-undo.


define variable l_file                       as character format "x(40)" no-undo.
define variable l_line                       as character   no-undo.


define variable l_error                      as character      no-undo.
define stream f_log.
define stream f_cim.
define variable v_quote               as character initial """"   no-undo.
define variable v_default             as character initial "-"    no-undo.
define variable v_space               as character initial " "    no-undo.
define variable v_log                 as character                no-undo.
define variable v_cim                 as character                no-undo.
define variable v_fline               as character                no-undo.
define variable v_errorstatus         as logical                  no-undo.
define variable v_toterrorstatus      as logical                  no-undo.
define variable v_errstring           as character                no-undo.
define variable v_status              as character                no-undo.
define variable v_existpod            as logical   initial no     no-undo.
define variable v_existpo             as logical   initial no     no-undo.
define variable v_poopen              as logical   initial no     no-undo.
define variable v_podopen             as logical   initial no     no-undo.
define variable v_runproagram         as character                no-undo.
define variable vc_traceid            as character                no-undo.
define variable vi_filecount          as integer                  no-undo.
define variable vc_filename               as character                           no-undo.
define variable vc_topath                 as character                           no-undo.
define variable v_errorcnt                as integer                             no-undo.
define variable vi_reccount                as integer                             no-undo.

define temp-table ttdept no-undo

            field dpt_bdn_acct    like dpt_mstr.dpt_bdn_acct
            field dpt_bdn_cc      like dpt_mstr.dpt_bdn_cc
            field dpt_bdn_sub     like dpt_mstr.dpt_bdn_sub
            field dpt_bvar_acc    like dpt_mstr.dpt_bvar_acc
            field dpt_bvar_cc     like dpt_mstr.dpt_bvar_cc
            field dpt_bvar_sub    like dpt_mstr.dpt_bvar_sub
            field dpt_bvrr_acc    like dpt_mstr.dpt_bvrr_acc
            field dpt_bvrr_cc     like dpt_mstr.dpt_bvrr_cc
            field dpt_bvrr_sub    like dpt_mstr.dpt_bvrr_sub
            field dpt_cop_acct    like dpt_mstr.dpt_cop_acct
            field dpt_cop_cc      like dpt_mstr.dpt_cop_cc
            field dpt_cop_sub     like dpt_mstr.dpt_cop_sub
            field dpt_dept        like dpt_mstr.dpt_dept
            field dpt_desc        like dpt_mstr.dpt_desc
            field dpt_domain      like dpt_mstr.dpt_domain
            field dpt_lbr_acct    like dpt_mstr.dpt_lbr_acct
            field dpt_lbr_cap     like dpt_mstr.dpt_lbr_cap
            field dpt_lbr_cc      like dpt_mstr.dpt_lbr_cc
            field dpt_lbr_sub     like dpt_mstr.dpt_lbr_sub
            field dpt_lvar_acc    like dpt_mstr.dpt_lvar_acc
            field dpt_lvar_cc     like dpt_mstr.dpt_lvar_cc
            field dpt_lvar_sub    like dpt_mstr.dpt_lvar_sub
            field dpt_lvrr_acc    like dpt_mstr.dpt_lvrr_acc
            field dpt_lvrr_cc     like dpt_mstr.dpt_lvrr_cc
            field dpt_lvrr_sub    like dpt_mstr.dpt_lvrr_sub
            field dpt_mod_date    like dpt_mstr.dpt_mod_date
            field dpt_user1       like dpt_mstr.dpt_user1
            field dpt_user2       like dpt_mstr.dpt_user2
            field dpt_userid      like dpt_mstr.dpt_userid
            field dpt_traceid     as character
            field dpt_dataid      as character
            field dpt_billnbr     as character
            field dpt_cimfile     as character
            field dpt_logfile     as character


                        
            index idx1 dpt_dept . 



   DEFINE DATASET dsDepartment FOR ttdept.
               
            hdataset = dataset dsDepartment:HANDLE.             

  


mainloop:
repeat on error undo mainloop, retry mainloop:
   /* Output and Batch Input */
   
   {xxworkpath_e.i &outbound=YES &inbound=yes &archive=YES}
   
   assign
      m_outpath  = m_outpath + "ro/"
      m_archpath = m_archpath + "ro/"
      m_successpath = m_successpath + "ro/"
      m_errorpath = m_errorpath + "ro/"
      m_workpath = m_workpath + "ro/".
   
 
      empty temp-table ttdept
      .
      v_runproagram = "".

     vc_traceid = "".
   /* Load item master data from JSON  */ 
   vc_outputjson = replace(vc_inputjson,".json",".run.out.json").   
   run loadjsonfile(input vc_inputjson,
                    input vc_outputjson,
                    output vl_RetOK).
   if vl_RetOK then
   do:
      find first ttdept no-error.
      assign vc_traceid = ttdept.dpt_traceid.
   end.
   
  
   if vc_import and vl_RetOK then 
   do:      
 

   for each ttdept break by ttdept.dpt_billnbr .
    if first-of(ttdept.dpt_billnbr) then 
    do:
      v_cim = m_workpath + "DPT" + "_" + ttdept.dpt_billnbr + "_" + string(ttdept.dpt_dept) + "_" + {&TimeStamp1} + "_" + {&TimeStamp2}  + ".cim".
      v_log = m_workpath + "DPT" + "_" + ttdept.dpt_billnbr + "_" + string(ttdept.dpt_dept) + "_" + {&TimeStamp1} + "_" + {&TimeStamp2}  + ".log".
      output stream f_cim to value(v_cim).
     end.
     
       put stream f_cim unformatted v_quote ttdept.dpt_dept v_quote skip   /* Department */
                                  v_default skip
                                  v_quote ttdept.dpt_desc    v_quote v_space
                                  v_quote ttdept.dpt_lbr_cap v_quote v_space
                                  v_quote ttdept.dpt_cop_acct v_quote v_space
                                  v_quote ttdept.dpt_cop_sub v_quote v_space
                                  v_quote ttdept.dpt_cop_cc v_quote v_space
                                  v_quote ttdept.dpt_lbr_acct v_quote v_space
                                  v_quote ttdept.dpt_lbr_sub v_quote v_space
                                  v_quote ttdept.dpt_lbr_cc v_quote v_space
                                  v_quote ttdept.dpt_bdn_acct v_quote v_space
                                  v_quote ttdept.dpt_bdn_sub v_quote v_space
                                  v_quote ttdept.dpt_bdn_cc v_quote v_space
                                  v_quote ttdept.dpt_lvar_acc v_quote v_space
                                  v_quote ttdept.dpt_lvar_sub v_quote v_space
                                  v_quote ttdept.dpt_lvar_cc v_quote v_space
                                  v_quote ttdept.dpt_lvrr_acc v_quote v_space
                                  v_quote ttdept.dpt_lvrr_sub v_quote v_space
                                  v_quote ttdept.dpt_lvrr_cc v_quote v_space
                                  v_quote ttdept.dpt_bvar_acc v_quote v_space
                                  v_quote ttdept.dpt_bvar_sub v_quote v_space
                                  v_quote ttdept.dpt_bvar_cc v_quote v_space
                                  v_quote ttdept.dpt_bvrr_acc v_quote v_space
                                  v_quote ttdept.dpt_bvrr_sub v_quote v_space
                                  v_quote ttdept.dpt_bvrr_cc v_quote v_space 
                                  skip.
     
      
      
      
      
      
     if last-of (ttdept.dpt_billnbr) then 
     do:     
      output stream f_cim close.
          

        
            v_runproagram = "rwdpmt.p".
            
   cimloadprocess:
   do transaction on error undo cimloadprocess, leave cimloadprocess:       
         
         run cimloadprocess(input v_cim,
                            input v_log,
                            input v_runproagram,
                            input vc_traceid,
                            input ttdept.dpt_billnbr,
                            input ttdept.dpt_dept,
                            output v_errstring,
                            output v_errorstatus) .
							
		v_toterrorstatus = v_toterrorstatus or v_errorstatus.  	
   
        
           

     /* Handle cim and log file */
        do vi_filecount = 1 to 2:
          /* when success move cim log file to archive directory  */
          /* when failed move cim log file to error directory  */   
          /*  move status json file to out directory for API return */   
          /* when success move previous step .p and json file to archive directory  */
          /* when failed move previous step .p and json file to error directory  */       
          case vi_filecount:
          when 1 then
          assign vc_filename = v_cim
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath.
          when 2 then
          assign vc_filename = v_log
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath.
           end case.           
          run cimfilehandle(input vc_filename,
                            input vc_topath,
                            input no ).         

                                      
                 
       end. /* do vi_filecount = 1 to 2: */   
         
         assign ttdept.dpt_cimfile = replace(v_cim,m_workpath,vc_topath)
                ttdept.dpt_logfile = replace(v_log,m_workpath,vc_topath).

         
             if  v_errorstatus then 
          
          undo cimloadprocess, leave cimloadprocess.      
         
   end. /* do transaction on error undo cimloadprocess, leave cimloadprocess: */
      
           
       end. /*  if last-of(ttdept) then  */
       
      end. /* for each ttdept */

 end. /* if vc_import and vl_RetOK then  */
 
    for each ttdept break by ttdept.dpt_billnbr.
 
     if last-of(ttdept.dpt_billnbr) then do:
      vi_reccount = vi_reccount + 1.

            
       run CreateAPILogMaster(input vc_traceid,
                              input "createdepartment",
                              input "xxdeptapi.p",
                              input ttdept.dpt_cimfile,
                              input ttdept.dpt_logfile,
                              input vc_inputjson,
                              input ttdept.dpt_dept,
                              input "",
                              input "",
                              input vi_reccount).                            

      end. /* if last-of(ttdept.dpt_billnbr) then do: */
     
   
                                  


   
   end. /* for each . */
   
   
   
   
   
   
 if can-find(first api_status where api_status.return_status = "FAILED" ) then
       do:
       
       for each api_status where api_status.return_status = "SUCCESS".
         
         delete api_status.
         
       end.
       
  end.  /* if can-find(first api_status */
         

        /* Handle cimload related files */
         /* Create output json */                   
         run writejsonfile(input hresult,
                  input vc_outputjson,
                  input "FILE",
                  input True,
                  input "UTF-8",
                  output vl_RetOK,
                  output vc_errorstring).
                  
 
       do vi_filecount = 1 to 5:
          /* when success move cim log file to archive directory  */
          /* when failed move cim log file to error directory  */   
          /*  move status json file to out directory for API return */   
          /* when success move previous step .p and json file to archive directory  */
          /* when failed move previous step .p and json file to error directory  */       
          case vi_filecount:
      
          when 1 then
          assign vc_filename = replace(vc_inputjson,".json",".p")
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 2 then
          assign vc_filename = vc_inputjson
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 3 then
          assign vc_filename = replace(vc_inputjson,".json",".dat")
                 vc_topath   = if v_errorstatus then m_errorpath else m_archpath. 
          when 4 then
          assign vc_filename = replace(vc_inputjson,".json",".run.log")
                 vc_topath   = m_outpath.
          when 5 then
          assign vc_filename = replace(vc_inputjson,".json",".run.out.json")
                 vc_topath   =  m_outpath.
          end case.
          
         if vl_RetOK then 
         put unformatted vc_filename " " skip
                         vc_topath " " skip 
                         vi_filecount skip.
     
        
          
          run cimfilehandle(input vc_filename,
                            input vc_topath,
                            input no ).                 
                   
          
     
       end.       /* do vi_filecount = 1 to 5: */      
       
  leave .
end. /* repeat on error undo mainloop, retry mainloop: */



